﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialIn.Biz;
using SocialIn.Biz.Utils;

namespace SocialIn.Web.Members
{
    public partial class InputBaseInfo : System.Web.UI.Page
    {
        Join _join;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        private void Initialize()
        {

            userName.Value = string.Empty;
            password2.Value = string.Empty;
            email.Value = string.Empty;
            isChecked.Value = "-1";
        }

        protected void btnJoinOK_Click(object sender, EventArgs e)
        {
            string redirectUrl = "/Members/InputBaseInfo.aspx";

            if (!string.Equals(isChecked.Value, "0"))
            {
                this.ErrorMessage("아이디 중복체크를 해주세요!", redirectUrl);
                return;
            }

            if (string.IsNullOrEmpty(userID.Value))
            {
                this.ErrorMessage("아이디를 입력해주세요!", redirectUrl);
                return;
            }

            if (string.IsNullOrEmpty(userName.Value))
            {
                this.ErrorMessage("닉네임을 입력해주세요!", redirectUrl);
                return;
            }

            if (string.IsNullOrEmpty(password2.Value))
            {
                this.ErrorMessage("비밀번호를 입력해주세요!", redirectUrl);
                return;
            }

            if (string.IsNullOrEmpty(email.Value))
            {
                this.ErrorMessage("메일주소를 입력해주세요!", redirectUrl);
                return;
            }

            UserInfo uinfo = new UserInfo()
            {
                UserID = userID.Value,
                UserName = userName.Value,
                Password = password2.Value,
                Email = email.Value,
                UserType = hidJoinType.Value
            };

            _join = new Join();

            if (!_join.AddMember(uinfo))
            {
                ErrorMessage("잘못된 정보를 입력하셨습니다!", redirectUrl);
            }
            else
                Response.Redirect("/");
        }

        private void ErrorMessage(string message, string redirectUrl)
        {
            message = string.Format("<script language=javascript>alert('{0}'); location.href='{1}';</script>", message, redirectUrl);
            ClientScript.RegisterClientScriptBlock(this.GetType(), "join", message);
        }
    }
}